<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class MerchantOnboardingApiController extends Controller
{
    /**
     * 提交商家入驻申请
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function apply(Request $request): JsonResponse
    {
        try {
            // 验证请求数据
            $validated = $request->validate([
                'login_account' => 'required|string|max:50',
                'login_password' => 'required|string|min:6|max:20',
                'name' => 'required|string|max:100',
                'phone' => 'required|string|max:20',
                'store_name' => 'required|string|max:100',
                'category' => 'required|string|in:restaurant,retail,service,beauty,other',
                'city' => 'required|string|max:100',
                'address' => 'required|string|max:200',
                'business_hours_start' => 'required|string',
                'business_hours_end' => 'required|string',
                'storefront_photo' => 'required|string', // 图片URL
                'business_license' => 'required|string', // 图片URL
                'remarks' => 'nullable|string|max:500'
            ]);

            // 模拟生成申请ID和账号信息
            $applicationId = 'APP' . date('Ymd') . rand(1000, 9999);
            $shopAccount = 'shop' . rand(10000000, 99999999);
            $shopPassword = $shopAccount;

            // 模拟保存申请数据
            $application = [
                'id' => $applicationId,
                'status' => 'pending',
                'created_at' => now()->format('Y-m-d H:i:s'),
                'shop_account' => $shopAccount,
                'shop_password' => $shopPassword,
                'login_url' => 'http://yidui.jiujiefang7979.cn/'
            ];

            return response()->json([
                'code' => 200,
                'message' => '申请提交成功',
                'data' => [
                    'application_id' => $applicationId,
                    'status' => 'pending',
                    'message' => '您的申请已提交，我们将在3个工作日内审核',
                    'shop_info' => [
                        'login_url' => $application['login_url'],
                        'login_account' => $shopAccount,
                        'login_password' => $shopPassword
                    ]
                ]
            ]);

        } catch (\Illuminate\Validation\ValidationException $e) {
            return response()->json([
                'code' => 422,
                'message' => '数据验证失败',
                'data' => $e->errors()
            ], 422);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 查询申请状态
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function status(Request $request): JsonResponse
    {
        try {
            // 从请求体中获取申请ID
            $applicationId = $request->input('application_id');
            
            if (!$applicationId) {
                return response()->json([
                    'code' => 400,
                    'message' => '缺少申请ID参数',
                    'data' => null
                ], 400);
            }

            // 模拟申请状态数据
            $status = [
                'id' => $applicationId,
                'status' => 'approved', // pending, approved, rejected
                'status_text' => '审核通过',
                'submitted_at' => '2025-08-16 10:30:00',
                'reviewed_at' => '2025-08-16 15:20:00',
                'shop_info' => [
                    'login_url' => 'http://yidui.jiujiefang7979.cn/',
                    'login_account' => 'shop10086123',
                    'login_password' => 'shop10086123'
                ],
                'message' => '恭喜您，注册商家成功！'
            ];

            return response()->json([
                'code' => 200,
                'message' => '查询成功',
                'data' => $status
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 获取入驻协议
     *
     * @return JsonResponse
     */
    public function agreement(): JsonResponse
    {
        try {
            $agreement = [
                'title' => '商家入驻协议',
                'content' => '欢迎您加入我们的平台！本协议规定了商家在平台上的权利和义务...',
                'version' => '1.0',
                'updated_at' => '2025-01-01'
            ];

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $agreement
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 获取商家分类选项
     *
     * @return JsonResponse
     */
    public function categories(): JsonResponse
    {
        try {
            $categories = [
                ['id' => 'restaurant', 'name' => '餐饮美食', 'description' => '餐厅、小吃、饮品等'],
                ['id' => 'retail', 'name' => '零售百货', 'description' => '超市、便利店、服装店等'],
                ['id' => 'service', 'name' => '生活服务', 'description' => '美容、美发、按摩等'],
                ['id' => 'beauty', 'name' => '美容美甲', 'description' => '美容院、美甲店、SPA等'],
                ['id' => 'other', 'name' => '其他', 'description' => '其他类型商家']
            ];

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $categories
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }
}

